package day22;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class HomeWork {
}

class A3{
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(5);
        List<Integer> list = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(r.nextInt(3)+1);
        }
        long start = System.currentTimeMillis();
        for (int i = 0; i < list.size(); i++) {
            int index = i;
            es.execute(() -> {
                long TaskStart = System.currentTimeMillis();
                System.out.println("任务"+index+"正在执行！");
                try {
                    Thread.sleep((list.get(index))*1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                long TaskEnd = System.currentTimeMillis();
                System.out.println("任务"+index+"花费"+(TaskEnd-TaskStart));
            });
        }
        es.shutdown();
        while (!es.isTerminated()) {}
        long end = System.currentTimeMillis();
        System.out.println("总花费"+(end-start));
    }
}

class B3{
    static long count = 1;
    public static void main(String[] args) {
        B3 b = new B3();
        long time = b.CTP();
        long time1 = b.STE();
        if (time<time1){
            System.out.println("newCachedTreadPool性能更优");
        }
        else {
            System.out.println("newSingleTreadExecutor性能更优");
        }
    }
    long CTP(){
        count = 1;
        ExecutorService es = Executors.newCachedThreadPool();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            es.execute(() -> {
                for(int j = 100000; j>=1; j--) {
                    count=count*j;
                }
            });
        }
        es.shutdown();
        while (!es.isTerminated()) {}
        long end = System.currentTimeMillis();
        System.out.println("Cached:"+(end-start));
        return end-start;
    }
    long STE(){
        count = 1;
        ExecutorService es = Executors.newSingleThreadExecutor();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            es.execute(() -> {
                for(int j = 100000; j>=1; j--) {
                    count=count*j;
                }
            });
        }
        es.shutdown();
        while (!es.isTerminated()) {}
        long end = System.currentTimeMillis();
        System.out.println("Single:"+(end-start));
        return end-start;
    }
}

class A4{
    static AtomicInteger count = new AtomicInteger(0);
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
            ses.scheduleAtFixedRate(()->{
                count.getAndIncrement();
                System.out.println("正在执行任务"+count.get());
                LocalTime currentLocalDateTime = LocalTime.now();
                System.out.println("当前时间:" + currentLocalDateTime.format(formatter));
            },2,5, TimeUnit.SECONDS);
        while (count.get()<5) {}
        ses.shutdown();
    }
}